package com.evaluation_system.service.impl;

import com.evaluation_system.constant.MessageConstant;
import com.evaluation_system.exception.AccountNotFoundException;
import com.evaluation_system.exception.PasswordErrorException;
import com.evaluation_system.mapper.UserMapper;
import com.evaluation_system.pojo.DTO.LoginInfoDTO;
import com.evaluation_system.pojo.entity.LoginInfo;
import com.evaluation_system.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.security.auth.login.AccountLockedException;

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public LoginInfo uploaderlogin(LoginInfoDTO loginUser) {
        String username = loginUser.getUsername();
        String password = loginUser.getPassword();
        LoginInfo loginInfo = userMapper.uploaderbyusername(username);
        //2、处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if (loginInfo == null) {
            //账号不存在
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        //密码比对
        if (!password.equals(loginInfo.getPassword())) {
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }
        return loginInfo;
    }

    @Override
    public LoginInfo checkerlogin(LoginInfoDTO loginUser) {
        String username = loginUser.getUsername();
        String password = loginUser.getPassword();
        LoginInfo loginInfo = userMapper.checkerbyusername(username);
        //2、处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if (loginInfo == null) {
            //账号不存在
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        //密码比对
        if (!password.equals(loginInfo.getPassword())) {
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }
        return loginInfo;
    }

    @Override
    public LoginInfo evaluatorlogin(LoginInfoDTO loginUser) {
        String username = loginUser.getUsername();
        String password = loginUser.getPassword();
        LoginInfo loginInfo = userMapper.evaluatorbyusername(username);
        //2、处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if (loginInfo == null) {
            //账号不存在
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        //密码比对
        if (!password.equals(loginInfo.getPassword())) {
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }
        return loginInfo;
    }

    @Override
    public LoginInfo adminlogin(LoginInfoDTO loginUser) {
        String username = loginUser.getUsername();
        String password = loginUser.getPassword();
        LoginInfo loginInfo = userMapper.adminbyusername(username);
        //2、处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if (loginInfo == null) {
            //账号不存在
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        //密码比对
        if (!password.equals(loginInfo.getPassword())) {
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }
        return loginInfo;
    }
}
